package com.xf.zk.curator.example;

import com.xf.zk.curator.constant.ZkConstant;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;

public class DistributeAtomicInt {
	static String distributeInt = "/curator_recipes_distributedAtoInt";
	static CuratorFramework client = CuratorFrameworkFactory.builder().connectString(ZkConstant.SERVER)
			.retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();

	public static void main(String[] args) throws Exception {
		client.start();
		DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger(client, distributeInt,
				new RetryNTimes(3, 1000));
		atomicInteger.initialize(0);
		AtomicValue<Integer> rc = atomicInteger.add(8);
		System.out.println("succeed: " + rc.succeeded() + ",preValue" + rc.preValue() + ",postValue" + rc.postValue());
		AtomicValue<Integer> rc1 = atomicInteger.compareAndSet(2,10);
		System.out.println("succeed: " + rc1.succeeded() + ",preValue" + rc1.preValue() + ",postValue" + rc1.postValue());
	}
}
